/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinMsgRecevier;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinMsgRecevierService;

/**
 * 微信消息接收者Controller
 * @author 陈维乾
 * @version 2018-05-18
 */
@Controller
@RequestMapping(value = "${adminPath}/wechatmsg/weixinMsgRecevier")
public class WeixinMsgRecevierController extends BaseController {

	@Autowired
	private WeixinMsgRecevierService weixinMsgRecevierService;
	
	@ModelAttribute
	public WeixinMsgRecevier get(@RequestParam(required=false) String id) {
		WeixinMsgRecevier entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = weixinMsgRecevierService.get(id);
		}
		if (entity == null){
			entity = new WeixinMsgRecevier();
		}
		return entity;
	}
	
	@RequiresPermissions("wechatmsg:weixinMsgRecevier:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeixinMsgRecevier weixinMsgRecevier, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WeixinMsgRecevier> page = weixinMsgRecevierService.findPage(new Page<WeixinMsgRecevier>(request, response), weixinMsgRecevier); 
		model.addAttribute("page", page);
		return "modules/wechatmsg/weixinMsgRecevierList";
	}

	@RequiresPermissions("wechatmsg:weixinMsgRecevier:view")
	@RequestMapping(value = "form")
	public String form(WeixinMsgRecevier weixinMsgRecevier, Model model) {
		model.addAttribute("weixinMsgRecevier", weixinMsgRecevier);
		return "modules/wechatmsg/weixinMsgRecevierForm";
	}

	@RequiresPermissions("wechatmsg:weixinMsgRecevier:edit")
	@RequestMapping(value = "save")
	public String save(WeixinMsgRecevier weixinMsgRecevier, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, weixinMsgRecevier)){
			return form(weixinMsgRecevier, model);
		}
		weixinMsgRecevierService.save(weixinMsgRecevier);
		addMessage(redirectAttributes, "保存微信消息接收者成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinMsgRecevier/?repage";
	}
	
	@RequiresPermissions("wechatmsg:weixinMsgRecevier:edit")
	@RequestMapping(value = "delete")
	public String delete(WeixinMsgRecevier weixinMsgRecevier, RedirectAttributes redirectAttributes) {
		weixinMsgRecevierService.delete(weixinMsgRecevier);
		addMessage(redirectAttributes, "删除微信消息接收者成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinMsgRecevier/?repage";
	}

}